//
//  JianjieViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/7.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "JianjieViewController.h"
#import "CZTextView.h"
@interface JianjieViewController ()<UITextViewDelegate>
@property (weak, nonatomic)  CZTextView *jianjie;

@end

@implementation JianjieViewController

/**
 *  初始化方法
 *
 *  @return 该类对象
 */
+(instancetype)viewcontroller{
    JianjieViewController *jjvc= [XHMianStory instantiateViewControllerWithIdentifier:@"jianjie"];
    return jjvc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTextView];
    // Do any additional setup after loading the view.
  
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.navigationItem.title=self.navtitle?self.navtitle:@"一句话简介";
   
    self.view.backgroundColor=UIColorWithRGB(243, 241, 241);
    if (self.text.length>0) {
        self.jianjie.text=self.text;
        self.jianjie.hidePlaceHolder=YES;
    }else{
        self.jianjie.hidePlaceHolder=NO;

    }
  
    
}
- (IBAction)save:(id)sender {
    
    if (self.mode&&![self.jianjie.text isEqualToString: self.text]) {
        self.text=self.jianjie.text;
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在保存";
        
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if ([self.mode.power integerValue]<5) {
           
            if (self.type==TYPEPEIZTJJ) {
                params[@"type"]=@"person";
                params[@"user_id"]=self.mode.user_id;
                params[@"action"]=@"edit_display";
                params[@"msg"]=self.jianjie.text;
            }else{
              params[@"user_type"]=@"person";
              params[@"user_id"]=self.mode.user_id;
            }
            if (self.type==TYPEJIANJIE) {
                params[@"action"]=@"p_intro_change";
                params[@"intro"]=self.jianjie.text;
            }else if (self.type==TYPELINK){
                
            }else if (self.type==TYPEPEIZTJJ){
                params[@"action"]=@"edit_display";
                params[@"msg"]=self.jianjie.text;
            }
            
        }else{
            
            if (self.type==TYPEPEIZTJJ) {
                params[@"user_id"]=self.mode.com_id;
                params[@"type"]=@"company";
                params[@"action"]=@"edit_display";
                params[@"msg"]=self.jianjie.text;
                if ([self.mode.power integerValue]==8) {
                    params[@"pid"]=self.mode.parent_id;
                }
            }else{
               params[@"com_id"]=self.mode.com_id;
                params[@"user_type"]=@"company";
            }
            if (self.type==TYPEJIANJIE) {
                params[@"action"]=@"c_shopjj_change";
                params[@"intro"]=self.jianjie.text;
                
            }else if(self.type==TYPEADDRESS){
                params[@"action"]=@"c_address_change";
                params[@"address"]=self.jianjie.text;
            }
            
        }
        
        NSString *url;
        if (self.type==TYPEPEIZTJJ) {
            url=myzturl;
        }else{
            url=userinfourl;
        }
        
        [PostUitls Post:url parameters:params success:^(id response) {
            
            NSDictionary *dict=response;
            NSNumber *state=dict[@"state"];
            if ([state integerValue]==400) {
                hud.mode=MBProgressHUDModeText;
                hud.labelText=@"保存成功";
                
                [self.navigationController popViewControllerAnimated:YES];
                if (self.block) {
                    if (![self.jianjie.text isEqualToString:@""]) {
                        self.block(self.jianjie.text);
                    }
                }
                
            }else{
                hud.mode=MBProgressHUDModeText;
                hud.labelText=@"保存失败";
                
            }
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.labelText=@"网络错误";
            [hud hide:YES];
        }];
    }else{
        
        if (self.block) {
            if (![self.jianjie.text isEqualToString:@""]) {
                self.block(self.jianjie.text);
                
            }else{
                self.block(nil);
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
   
    
}
- (IBAction)cancel:(id)sender {
 
     [self.navigationController popViewControllerAnimated:YES];

}
-(void)setUpTextView{
    CZTextView *jianjie = [[CZTextView alloc] initWithFrame:
                           CGRectMake(16, 84, XHScreenwidth-32, 160)];
    _jianjie=jianjie;
    // 设置占位符
    _jianjie.placeHolder =self.textlength>0?[NSString stringWithFormat:@"字数输入1-%ld字以内",self.textlength]: @"字数输入1-50字以内";
    _jianjie.placehodercolor=UIColorWithRGB(217, 211, 215);
    _jianjie.font = [UIFont systemFontOfSize:14];
    _jianjie.textColor=[UIColor grayColor];
    _jianjie.layer.borderColor=[UIColorWithRGB(206, 212, 211)CGColor];
    _jianjie.layer.cornerRadius=3;
    _jianjie.layer.borderWidth=1;
    [self.view addSubview:jianjie];
    
    // 默认允许垂直方向拖拽
    jianjie.alwaysBounceVertical = YES;
    
    // 监听文本框的输入
    /**
     *  Observer:谁需要监听通知
     *  name：监听的通知的名称
     *  object：监听谁发送的通知，nil:表示谁发送我都监听
     *
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
    
    // 监听拖拽
    _jianjie.delegate = self;

}
- (void)textChange
{
    // 判断下textView有木有内容
    if (_jianjie.text.length>0) { // 有内容
        _jianjie.hidePlaceHolder = YES;
    }else{
        _jianjie.hidePlaceHolder = NO;
    }
}


-(void)rightclick{
       }
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)textViewDidChange:(UITextView *)textView{
    //该判断用于联想输入
    NSInteger num;
    if (self.textlength>0) {
        num=self.textlength;
    }else{
        num=50;
    }
    
    if (textView.text.length >num)
    {
        textView.text = [textView.text substringToIndex:num];
    }
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
   
    if ([text isEqualToString:@"\n"]) {
        
        return NO;
    }
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.jianjie endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
